import re
import time

import requests

from config import BASE_PATH


def read_data_from_txt(file_path):
    data_dict = {}
    with open(file_path, 'r', encoding='utf-8') as file:
        for line in file:
            # 去除每行首尾的空白字符（包括换行符）
            line = line.strip()
            # 分割每行数据，假设每行数据由空格分隔
            key, value = line.split(' ', 1)
            # 将键值对存入字典
            data_dict[key] = int(value)  # 假设值都是整数，故转换为int类型
    return data_dict


def inform():

    url = "https://open.feishu.cn/open-apis/bot/v2/hook/f062757b-b914-4e19-bd92-a1c2447dc45a"
    # 文件路径示例
    file_path = BASE_PATH+'\\report\export\prometheusData.txt'  # 替换为你的文件路径
    launch_data = read_data_from_txt(file_path)
    current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())


    method = 'post'
    headers = {
        'Content-Type': 'application/json'
    }

    json = {
        "msg_type":"text",
        "content":{
            "text":
                "项目名称：MES小程序\n"
                f"运行测试用例总数：{launch_data.get('launch_retries_run')} \n"
                f"通过数量：{launch_data.get('launch_status_passed')} \n"
                f"失败用例数：{launch_data.get('launch_status_failed')} \n"
                f"跳过用例数：{launch_data.get('launch_status_skipped')} \n"
                f"测试用例重跑：{launch_data.get('launch_retries_retries')} \n"
                f"总持续时长：{launch_data.get('launch_time_duration') / 1000} 秒 \n"
                f"运行时间：{current_time}",

                    "tag": "lark_md"
                   }}

    rep = requests.request(method=method, url=url, headers=headers,json=json)



# if __name__ == '__main__':
#     inform()

